/*
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 */

package ru.ssau.berlin.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Caesar
 */
@Entity
@NamedQueries(
    {@NamedQuery(
        name = "findPersonTypes",
        query = "SELECT object(pt) FROM PersonType pt"
    ),
    @NamedQuery(
       name = "findPersonTypeByName",
       query = "SELECT object(pt) FROM PersonType pt WHERE pt.name LIKE :typeName"
    ),
    @NamedQuery(
        name = "findPersonTypeByParent",
        query = "SELECT object(pt) FROM PersonType pt WHERE pt.parent.id LIKE :parentId"
    )}
)
public class PersonType implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToOne
    private PersonType parent;

    @OneToMany(mappedBy="parent", cascade=CascadeType.REMOVE)
    private List<PersonType> children;

    @OneToMany(mappedBy="type")
    private List<Person> persons;

    @ManyToMany(fetch=FetchType.EAGER)
    private List<PersonAttribute> attributes;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public PersonType getParent() {
        return parent;
    }

    public void setParent(PersonType parent) {
        this.parent = parent;
    }

    public List<PersonAttribute> getAttributes() {
        return attributes;
    }

    public List<Person> getPersons() {
        return persons;
    }

    public List<PersonType> getChildren() {
        return children;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof PersonType)) {
            return false;
        }
        PersonType other = (PersonType) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return this.name + "[" + this.id + "]";
    }
}
